﻿using MediatR;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class UpdateFeedHandler : IRequestHandler<UpdateFeedCommand, ApiResult<int>>
    {
        public UpdateFeedHandler(IBaseRepository<FeedModel> feedRepository)
        {
            FeedRepository = feedRepository;
        }

        public IBaseRepository<FeedModel> FeedRepository { get; }

        /// <summary>
        /// 修改养殖信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateFeedCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<int>();

            var feed=await FeedRepository.GetValueAsync(request.Id);
            feed.FeedVariety=request.FeedVariety;
            feed.Pic = request.Pic;
            feed.FeedDays=request.FeedDays;
            feed.FeedSource=request.FeedSource;
            feed.FeedFoodName=request.FeedFoodName;
            feed.FeedName=request.FeedName;
            feed.LeaveDate=request.LeaveDate;
            feed.CompaniesId=request.CompaniesId;

            var update=await FeedRepository.UpdateAsync(feed);
            if(update>0)
            {
                res.Code = EnumCode.Success;
                res.Message = "修改养殖信息成功";
                res.Data = update;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "修改养殖信息失败";
                res.Data = update;
                return res;
            }
        }
    }
}
